package com.avic.rong.userOrder.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.userOrder.adapter.HistoryLookListAdapter;
import com.avic.rong.userOrder.api.HistoryLookApi;
import com.avic.rong.userOrder.bean.HistoryLookBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HistoryLookActivity extends BaseActivity {

    @BindView(R.id.history_list)
    RecyclerView historyList;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;
    private ArrayList<HistoryLookBean> listData=new ArrayList<>();
    private HistoryLookListAdapter historyLookListAdapter;
    private int taiTouID;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_history_look;
    }

    @Override
    protected void initView() {
        taiTouID = getIntent().getIntExtra("taiTouID", -1);
        tvTitle.setText("历史抬头");
        historyList.setLayoutManager(new LinearLayoutManager(this));
        historyLookListAdapter = new HistoryLookListAdapter(this,listData,taiTouID);
        historyList.setAdapter(historyLookListAdapter);

        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()){
            final HistoryLookApi historyLookApi = new HistoryLookApi(this, simpleOnNextListener);
            historyLookApi.setCache(false);

            HttpManager.getInstance().doHttpDeal(historyLookApi);

            loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(historyLookApi);
                    loading_layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    onBackPressed();
                }
            });
            loading_layout.setStatus(LoadingLayout.LOADING);
        } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }

    }

    @Override
    protected void addListener() {

    }

    //权威评测
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            listData.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(HistoryLookActivity.this,subjects.getMsg());
                return;
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            List<HistoryLookBean> data = (List<HistoryLookBean>) subjects.getData();
            listData.addAll(data);
            historyLookListAdapter.notifyDataSetChanged();
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx",e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };


}
